﻿Imports JPC.Medialibrary.DAL
Imports JPC.Medialibrary.BLL
Imports JPC.Medialibrary.UI
Imports System.Diagnostics

Partial Class Series
    Inherits System.Web.UI.Page

    Private _noResults As Boolean = False

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        System.Threading.Thread.Sleep(80)

        If Page.IsPostBack Then
            If Mid(Request("__EVENTARGUMENT"), 1, 4) = "@@@@" Then
                MySession.ListViewMode = eListViewMode.Thumbnail

                Select Case MySession.ListViewMode
                    Case eListViewMode.List
                        SelectMenu(btnList, eListViewMode.List)
                    Case eListViewMode.Thumbnail
                        SelectMenu(btnThumbnail, eListViewMode.Thumbnail)
                End Select

                'ProcesarComando("Lista", "InspectDirectory", "")
                'Breadcrumb.AddItem("Lista", "InspectDirectory", "")
                'Breadcrumb.Bind(lblLastBreadcrumb, lstBreadcrumb)
                'lstMain.DataSource = New FilesMenu("", "Series")
                'lstMain.DataBind()

            Else
                Breadcrumb.Bind(lblLastBreadcrumb, lstBreadcrumb)
                If Me.cmdName.Value <> "" Then
                    ProcesarComando(Me.btnText.Value, Me.cmdName.Value, Me.cmdArg.Value)
                    Me.cmdName.Value = ""
                    Me.cmdArg.Value = ""
                    Me.btnText.Value = ""
                End If
            End If
        Else
            MySession.BreadCrumbList = Nothing
            MySession.LastBreadcrumb = Nothing
            imgSerie.Visible = False
        End If

    End Sub

    Private Sub ProcesarComando(ByVal Text As String, ByVal CommandName As String, ByVal CommandArgument As String)

        CommandArgument = CommandArgument.Replace("?", "\").Replace("*", "'")

        Select Case CommandName
            Case "Inicio"
                Response.Redirect("~/Start.aspx")

            Case "PlayVideo"
                SeriesManager.AddEpisodeToHistory(CommandArgument)

                Dim pr As Process = New Process
                pr.StartInfo.FileName = """C:\Program Files\Zoom Player\zplayer.exe"""
                pr.StartInfo.Arguments = """" + CommandArgument + """" + " /F /LOCKFS /Q /MEDIA /MOUSEOFF /CLOSE"
                pr.Start()

                System.Threading.Thread.Sleep(5000)

                'Dim oShell As Object
                'Dim lRet As Integer
                'Dim filename As String = """C:\Program Files\Zoom Player\zplayer.exe"""
                'Dim arg As String = """" + CommandArgument + """" + " /F /LOCKFS /Q /MEDIA /MOUSEOFF /CLOSE"
                'oShell = Server.CreateObject("WScript.Shell")
                'lRet = oShell.Run(filename & " " & arg, 1, True)

                lstMain.DataSource = New FilesMenu(Mid(CommandArgument, 1, InStrRev(CommandArgument, "\")), "Series")
                lstMain.DataBind()

            Case "InspectDirectory"
                ulMenu.Visible = CommandArgument = ""
                imgSerie.Visible = Not ulMenu.Visible

                If imgSerie.Visible Then
                    Dim thumbFileName As String = "~\Images\Video_Thumbnail.jpg"
                    If My.Computer.FileSystem.FileExists(CommandArgument & "\Thumb.jpg") Then
                        thumbFileName = "Handlers/GetThumbnail.ashx?" & Server.UrlEncode(CommandArgument & "\Thumb.jpg")
                    End If
                    imgSerie.ImageUrl = thumbFileName
                End If

                Breadcrumb.AddItem(Text, CommandName, CommandArgument)
                Breadcrumb.Bind(lblLastBreadcrumb, lstBreadcrumb)

                If MySession.ListViewMode = eListViewMode.List Or CommandArgument <> "" Then
                    lstThumbnails.Visible = False
                    lstMain.Visible = True
                    lstMain.DataSource = New FilesMenu(CommandArgument, "Series")
                    lstMain.DataBind()
                Else
                    lstMain.Visible = False
                    lstThumbnails.Visible = True
                    lstThumbnails.DataSource = New FilesMenu(CommandArgument, "Series")
                    lstThumbnails.DataBind()
                End If
        End Select

    End Sub

    Protected Sub lstMain_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lstMain.ItemDataBound

        If (e.Item.ItemType = ListViewItemType.DataItem) Then
            Dim btn As LinkButton = CType(e.Item.FindControl("btnMain"), LinkButton)
            Dim lbl As Label = CType(e.Item.FindControl("lblInfo"), Label)

            Dim dataItem As Object = CType(e.Item, ListViewDataItem).DataItem
            Dim pnlRow As Panel = CType(e.Item.FindControl("pnlRow"), Panel)

            Dim btnId As String = "'" + btn.ClientID + "'"

            Dim _listItem As ListItem = CType(dataItem, ListItem)
            btn.OnClientClick = "javascript:setAction('" + _listItem.CommandName.Trim + "','" + _listItem.CommandArgument.Trim.Replace("\", "?").Replace("'", "*") + "'," + btnId + ");"
            btn.Text = _listItem.Text.Trim
            If _listItem.Info <> "" And _listItem.CommandName = "PlayVideo" Then
                pnlRow.CssClass = pnlRow.CssClass + " viewed"
            End If
            lbl.Text = _listItem.Info

            If _noResults Then
                'pnlRow.CssClass = "row"
                btn.OnClientClick = "javascript:return false;"
            End If
        End If

    End Sub

    Protected Sub lstBreadcrumb_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lstBreadcrumb.ItemDataBound

        Breadcrumb.ItemDataBound(sender, e)

    End Sub

    Protected Sub lstBreadcrumb_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lstBreadcrumb.ItemCommand

        Dim index As Integer = CInt(e.CommandArgument)
        Dim li As ListItem = MySession.BreadCrumbList(index)
        Breadcrumb.RemoveRange(index)
        ProcesarComando(li.Text, li.CommandName, li.CommandArgument)

    End Sub

    Protected Function getPostBack() As String
        Return ClientScript.GetPostBackEventReference(Me, "@@@@")
    End Function

    Protected Sub lstThumbnails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lstThumbnails.ItemDataBound

        If (e.Item.ItemType = ListViewItemType.DataItem) Then
            Dim img As ImageButton = CType(e.Item.FindControl("imgThumbnail"), ImageButton)
            Dim lbl As Label = CType(e.Item.FindControl("lblThumbnailText"), Label)

            Dim dataItem As Object = CType(e.Item, ListViewDataItem).DataItem
            Dim _listItem As ListItem = CType(dataItem, ListItem)

            Dim lblId As String = "'" + lbl.ClientID + "'"

            img.OnClientClick = "javascript:setAction('" + _listItem.CommandName.Trim + "','" + _listItem.CommandArgument.Trim.Replace("\", "?").Replace("'", "*") + "'," + lblId + ");"
            lbl.Text = _listItem.Text.Trim
            img.ImageUrl = "Handlers/GetThumbnail.ashx?" & Server.UrlEncode(_listItem.ThumbnailURL) '.Replace(" ", "%20")

            If _noResults Then
                'pnlRow.CssClass = "row"
                'btn.OnClientClick = "javascript:return false;"
            End If
        End If

    End Sub

    Protected Sub btnList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnList.Click

        SelectMenu(btnList, eListViewMode.List)

    End Sub

    Protected Sub btnThumbnail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnThumbnail.Click

        SelectMenu(btnThumbnail, eListViewMode.Thumbnail)

    End Sub

    Private Sub SelectMenu(ByVal btn As LinkButton, ByVal ListviewMode As eListViewMode)

        MySession.ListViewMode = ListviewMode
        btnList.CssClass = ""
        btnThumbnail.CssClass = ""
        btn.CssClass = "selected"
        ProcesarComando("Lista", "InspectDirectory", "")

    End Sub

End Class